package com.atguigu.day09;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Json;
import org.apache.flink.table.descriptors.Kafka;
import org.apache.flink.table.descriptors.Schema;

public class Flink06_SQL_Demo {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        DataStreamSource<WaterSensor> waterSensorStream =
                env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                        new WaterSensor("sensor_1", 2000L, 20),
                        new WaterSensor("sensor_2", 3000L, 30),
                        new WaterSensor("sensor_1", 4000L, 40),
                        new WaterSensor("sensor_1", 5000L, 50),
                        new WaterSensor("sensor_2", 6000L, 60));

        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //3.将流转为表(未注册的表)
//        Table table = tableEnv.fromDataStream(waterSensorStream);

        //将Table对象注册
//        tableEnv.createTemporaryView("sensor", table);


        //直接在流转为表的时候注册
        tableEnv.createTemporaryView("sensor", waterSensorStream);
        //如果需要Table对象，还可以通过已注册的表名获取Table对象
//        Table sensor = tableEnv.from("sensor");

        //4.通sql查询未注册表的数据
//        tableEnv.executeSql("select * from "+table+" where id='sensor_1'").print();
        //查询已注册的表
        tableEnv.executeSql("select * from sensor where id='sensor_1'").print();

    }
}
